﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MD5Hash;
using WindowsFormsApp1.Respostories;
using WindowsFormsApp1.Utils;

namespace WindowsFormsApp1
{
    public partial class ResetPwd : Form
    {
        public ResetPwd()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void btSave_Click(object sender, EventArgs e)
        {
            //判断数据有效性
            if (txtOldPwd.Text == ""  || txtOldPwd.Text.Length < 4 || txtOldPwd.Text.Length > 12 || !txtOldPwd.Text.All(char.IsDigit))
            {
                lbOldPwdWarn.Text = "请输入有效的旧密码！";
                //系统响铃提示
                System.Media.SystemSounds.Beep.Play();
                return;
            }
            //判断旧密码是否正确
            if(txtOldPwd.Text.GetMD5() != GlobalData.AdminUser.Password)
            {
                lbOldPwdWarn.Text = " 旧密码错误！";
                System.Media.SystemSounds.Beep.Play();
                return;
            }

            //新密码必须4-12位数字
            if (txtNewPwd.Text == "" || txtNewPwd.Text.Length < 4 || txtNewPwd.Text.Length > 12 || !txtNewPwd.Text.All(char.IsDigit))
            {
                lbNewPwdWarn.Text = "请输入有效的新密码！";
                //系统响铃提示
                System.Media.SystemSounds.Beep.Play();
                return;
            }

            if (txtNewPwd.Text != txtNewPwd_1.Text)
            {
                lbNewPwd_1Warn.Text = "两次输入的密码不一致！";
                System.Media.SystemSounds.Beep.Play();
                return;
            }
            //修改密码

            bool result = UserRespostoriy.updatePassword(GlobalData.AdminUser, txtNewPwd.Text);//修改密码
            if (result)
            {
                MessageBox.Show("密码修改成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("密码修改失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 关闭窗口
            this.Close();
        }
    }
}
